package com.hainatong.shiphelper.mainhome.fragment;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.StringUtils;
import com.bumptech.glide.Glide;
import com.hainatong.shiphelper.Constants;
import com.hainatong.shiphelper.GlobalConfig;
import com.hainatong.shiphelper.MvvmLazyFragment;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.FragmentWelcomeBinding;
import com.hainatong.shiphelper.mainhome.ComFragmentActivity;
import com.hainatong.shiphelper.mainhome.adapter.FunctionAdapter;
import com.hainatong.shiphelper.mainhome.entity.FunctionEntity;
import com.hainatong.shiphelper.module.workermgr.WorkerMgrActivity;
import com.hainatong.shiphelper.utils.ClickUtil;
import com.hainatong.mvvmbase.base.NoViewModel;
import com.hainatong.mvvmbase.utils.StatusBarUtils;

import android.view.View;
import androidx.recyclerview.widget.LinearLayoutManager;

public class WelcomeFragment extends MvvmLazyFragment<FragmentWelcomeBinding, NoViewModel>
{
    private List<FunctionEntity> functions;


    @Override
    protected void initParameters()
    {
        // 设置透明状态栏，兼容4.4
        // 设置状态栏为白底黑字
        //StatusBarUtils.setStatusBarColor(getActivity(), Color.parseColor("#265ceb"));
        StatusBarUtils.setStatusBarDarkFont(getActivity(), false);
    }

    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_welcome;
    }

    @Override
    public int getBindingVariable()
    {
        return 0;
    }

    @Override
    protected void onRetryBtnClick()
    {

    }

    @Override
    protected void initView(View view)
    {
        viewDataBinding.scrollview.scrollTo(0,0);

        functions = new ArrayList<>();
        functions.add( new FunctionEntity( getString(R.string.stock_out_in_mgr),"出库，入库，领用记录",R.mipmap.ic_item_ruku ) );
        functions.add( new FunctionEntity( getString(R.string.xiujia_mgr),"正常，临时，延期等休假",R.mipmap.ic_item_xiujia ) );
        functions.add( new FunctionEntity( getString(R.string.worker_mgr_str),"人员相关的管理",R.mipmap.ic_item_worker ) );
        functions.add( new FunctionEntity( getString(R.string.work_mgr),"值班交接，上下船交接等",R.mipmap.ic_item_trans_work ) );
        functions.add( new FunctionEntity( getString(R.string.cert_mgr),"各类设备证件",R.mipmap.ic_item_passport ) );
        functions.add( new FunctionEntity( getString(R.string.qshe_mgr),"各类模板表格",R.mipmap.ic_item_qshe) );
        functions.add( new FunctionEntity( getString(R.string.oa_shenhe_mgr),"各类模板表格",R.mipmap.ic_oa_shenhe) );

        FunctionAdapter adapter = new FunctionAdapter(functions);
        viewDataBinding.recyclerview.setAdapter(adapter);
        viewDataBinding.recyclerview.setNestedScrollingEnabled(false);
        viewDataBinding.recyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
        viewDataBinding.recyclerview.setHasFixedSize(true);
        viewDataBinding.recyclerview.scrollToPosition(0);
        adapter.setOnItemClickListener( (adapter1, view1, position) -> {

            if (ClickUtil.isFastClick(500, view1))
            {
                return;
            }

            if ( adapter.getItem( position ).getTitle().equals( getString(R.string.stock_out_in_mgr) ) )
            {
                Bundle bundle = new Bundle();
                bundle.putString(Constants.FRAGMNET_TYPE , Constants.FUN_STOCK_OUT_IN_MGR);
                ComFragmentActivity.start(getActivity(),bundle);
            }
            else if( adapter.getItem( position ).getTitle().equals( getString(R.string.xiujia_mgr) ))
            {
                Bundle bundle = new Bundle();
                bundle.putString(Constants.FRAGMNET_TYPE , Constants.FUN_HOLIDAY_MGR);
                ComFragmentActivity.start(getActivity(),bundle);
            }

            else if( adapter.getItem( position ).getTitle().equals( getString(R.string.worker_mgr_str) ))
            {
                WorkerMgrActivity.start( getActivity() );
                //startActivity( new Intent(getActivity(), WorkerMgrActivity.class) );
            }
            else if( adapter.getItem( position ).getTitle().equals( getString(R.string.cert_mgr) ))
            {
                /*
                // scheme  传值 演示
                String uri = "luban://example/login?myArgs=87945645642";
                Intent luban = new Intent(Intent.ACTION_VIEW , Uri.parse( uri ));
                startActivity(luban);
                */

                Bundle bundle = new Bundle();
                bundle.putString(Constants.FRAGMNET_TYPE , Constants.FUN_CERT_MGR);
                ComFragmentActivity.start(getActivity(),bundle);
            }
            else if( adapter.getItem( position ).getTitle().equals( getString(R.string.work_mgr) ))
            {
                Bundle bundle = new Bundle();
                bundle.putString(Constants.FRAGMNET_TYPE , Constants.FUN_WORK_MGR);
                ComFragmentActivity.start(getActivity(),bundle);
            }
            else if( adapter.getItem( position ).getTitle().equals( getString(R.string.qshe_mgr) ))
            {
                Bundle bundle = new Bundle();
                bundle.putString(Constants.FRAGMNET_TYPE , Constants.FUN_QSHE_MGR);
                ComFragmentActivity.start(getActivity(),bundle);
            }
            else if( adapter.getItem( position ).getTitle().equals( getString(R.string.oa_shenhe_mgr) ))
            {
                //OA 审批
                ComFragmentActivity.startView(getActivity(),Constants.VIEW_OA_MANAGER);
            }
        } );


    }

    @Override
    protected void lazyLoad()
    {
         //有 用户的时候
         if(ObjectUtils.isNotEmpty(GlobalConfig.getInstance().getUserInfo()))
         {
             viewDataBinding.tvUserHello.setText( String.format( getString(R.string.good_morning), GlobalConfig.getInstance().getUserInfo().getUserName() ) );
             viewDataBinding.tvWelcomeBackTag.setText( getString( R.string.welcome_back_tag ) );


             //如果 有公司 logo 就显示 公司logo
             // 这里 应该显示 公司logo
             if(!StringUtils.isEmpty( GlobalConfig.getInstance().getUserInfo().getHeadImageUrl() ))
             {
                 //GlideUtil.showImg(viewDataBinding.ivCompanyLogo , GlobalConfig.getInstance().getUserInfo().getHeadImageUrl());

                 Glide.with(viewDataBinding.ivCompanyLogo.getContext())
                         .load(R.mipmap.ic_def_logo)
                         .into(viewDataBinding.ivCompanyLogo);
             }

         }

    }


}
